<?php
/**
 * Something Functions tests
 *
 * @package Extensions
 * @subpackage test
 * @copyright Damien 2007
 * @author Damien
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */


/**
 * LowCalVanilla_Cache class tests
 * @package Extensions
 */
class TestCache extends UnitTestCase {

	var $Context;
	var $Cache;

	function setUp() {
		$this->Context = new TestContext();
		$this->Cache = new LowCalVanilla_Cache($this->Context);
	}

	function testConstructor() {
		$this->assertEqual(TEST_DIR . '/_files/cache/', $this->Cache->_Dir);
		$this->assertTrue(file_exists($this->Cache->_Dir));
		$this->assertEqual(
			$this->Context->Configuration['LOWCALVANILLA_CACHE_EXT'],
			$this->Cache->_Ext);
	}

	function testAddTTL() {
		$this->assertTrue($this->Cache->Add('testTTL', 'string Test', 1));
		sleep(2);
		$this->assertTrue($this->Cache->Add('testTTL', 'string Test', 10));
		$this->assertFalse($this->Cache->Add('testTTL', 'string Test'));
		$this->Cache->ClearCache();
	}

	function testAddArray() {
		$Array = array("test" => 1);
		$this->assertTrue($this->Cache->Add('testArray', $Array));

		$File = new LowCalVanilla_File(
			$this->Context,
			$this->Context->Configuration['LOWCALVANILLA_CACHE_DIR']
				. md5('testArray')
				. $this->Context->Configuration['LOWCALVANILLA_CACHE_EXT']
		);
		$this->assertTrue($File->Exist());
		$Header = $File->GetContent(5, false);
		$Content = $File->GetContent();
		$this->assertEqual($Array, @unserialize($Content));

		$File->Unlink();
	}

	function testClearCache() {
		$this->assertTrue($this->Cache->Add('test1','test ClearCache()'));
		$this->assertTrue($this->Cache->Add('test2','test ClearCache() again'));
		$this->assertTrue($this->Cache->ClearCache());

		$File1 = new LowCalVanilla_File(
			$this->Context,
			$this->Context->Configuration['LOWCALVANILLA_CACHE_DIR']
				. md5('test1')
				. $this->Context->Configuration['LOWCALVANILLA_CACHE_EXT']
		);
		$this->assertFalse($File1->Exist());
		$File1->Unlink();

		$File2 = new LowCalVanilla_File(
			$this->Context,
			$this->Context->Configuration['LOWCALVANILLA_CACHE_DIR']
				. md5('test2')
				. $this->Context->Configuration['LOWCALVANILLA_CACHE_EXT']
		);
		$this->assertFalse($File2->Exist());
		$File2->Unlink();

	}

	function testDelete() {
		$this->assertTrue($this->Cache->Add('test1','test Delete()'));
		$this->assertTrue($this->Cache->Delete('test1'));

		$File1 = new LowCalVanilla_File(
			$this->Context,
			$this->Context->Configuration['LOWCALVANILLA_CACHE_DIR']
				. md5('test1')
				. $this->Context->Configuration['LOWCALVANILLA_CACHE_EXT']
		);
		$this->assertFalse($File1->Exist());
		$File1->Unlink();
	}

	function testFlesh() {
		$this->assertTrue($this->Cache->ClearCache());
		$this->assertFalse($this->Cache->Fletch('test'));

		$this->assertTrue($this->Cache->Add('test1','test Fletch()'));
		$this->assertEqual('test Fletch()', $this->Cache->Fletch('test1'));

		$Array = array("test" => 1);
		$this->assertTrue($this->Cache->Add('test2', $Array));
		$this->assertEqual($Array, $this->Cache->Fletch('test2'));

		$this->assertTrue($this->Cache->ClearCache());
	}

	function testFleshTTL() {
		$this->assertTrue($this->Cache->ClearCache());
		$this->assertTrue($this->Cache->Add('test1','test Fletch()', 1));
		sleep(2);
		$this->assertFalse($this->Cache->Fletch('test1'));

		$this->assertTrue($this->Cache->Add('test2','test Fletch() again', 10));
		$this->assertTrue($this->Cache->Fletch('test2'));
		$this->assertTrue($this->Cache->ClearCache());
	}

	function testStore() {
		$this->assertTrue($this->Cache->Store('testSting', 'test Store()'));
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->Context->Configuration['LOWCALVANILLA_CACHE_DIR']
				. md5('testSting')
				. $this->Context->Configuration['LOWCALVANILLA_CACHE_EXT']
		);
		$this->assertTrue($File->Exist());
		$Header = $File->GetContent(5, false);
		$Content = $File->GetContent();
		$this->assertEqual('test Store()', $Content);
		$this->assertFalse($this->Cache->Add('testSting', 'test Store()'));
		$this->assertTrue($this->Cache->Store('testSting', 'test Store()'));
		$File->Unlink();
	}
}

//$test = &new TestCache();
//$test->run(new TextReporter());